<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/10/9
 * Time: 11:02 AM
 */

namespace app\common\controller;


use app\common\model\DevicePrint;
use app\common\model\Paylog;
use feie\HttpClient;
use think\Controller;

class FeiePrint extends Controller
{
    public function sendprint($orderid)
    {
        $Paylog = new Paylog();
        $orderinfo = $Paylog->where('orderid', $orderid)->find();
        if ($orderinfo['pay_statu'] == 1) {
            $Print = new DevicePrint();
            $cfg = $Print->where(array('mid' => $orderinfo['mid']))->find();

            if ($cfg) {
                $user = $cfg['username'];
                $ukey = $cfg['ukey'];
                $stime = time();
                $sig = sha1($user . $ukey . $stime);
                $sn = $cfg['sn'];
                $orderInfo = "<CB>收款成功</CB><BR>";
                $orderInfo .= '--------------------------------<BR>';
                $orderInfo .= "门店:" . getmachnames($orderinfo['mid']) . '<BR><BR>';
                $orderInfo .= "订单号:<BR>" . $orderinfo['orderid'] . '<BR><BR>';
                $orderInfo .= "收单单号:<BR>" . $orderinfo['orderno'] . '<BR><BR>';
                $orderInfo .= "支付方式:" . getpayfs($orderinfo['pay_fs']) . '<BR>';
                $orderInfo .= '--------------------------------<BR>';
                $orderInfo .= "实收金额:<B>" . $orderinfo['price'] . '</B>元' . '<BR><BR>';
                $orderInfo .= "应收金额:" . $orderinfo['yf_price'] . '元' . '<BR><BR>';
                $orderInfo .= "折扣金额:" . $orderinfo['zk_price'] . '元' . '<BR><BR>';
                $orderInfo .= "消费时间:" . date("Y-m-d H:i:s", $orderinfo['add_time']) . '<BR><BR>';
                $orderInfo .= '--------------------------------<BR>';
                $orderInfo .= "技术支持:悟能科技(400-0372-203)" . '<BR><BR>';
                //$orderInfo .= ''.$_POST['riqi'].'<BR>';
//            halt($orderInfo);
                $this->wp_print($user, $stime, $sig, $sn, $orderInfo, 1);
            }
        }

    }

    private function wp_print($user, $stime, $sig, $printer_sn, $orderInfo, $times)
    {
        $ip = 'api.feieyun.cn';        //接口IP或域名
        $port = 80;                    //接口IP端口
        $hostname = '/Api/Open/';    //接口路径
        $content = array(
            'user' => $user,
            'stime' => $stime,
            'sig' => $sig,
            'apiname' => 'Open_printMsg',

            'sn' => $printer_sn,
            'content' => $orderInfo,
            'times' => $times//打印次数
        );
        $image = new HttpClient($ip, $port);
        if (!$image->post($hostname, $content)) {
            echo 'error';
        } else {
            header("Content-type: text/html; charset=utf-8");
            $s = $image->getContent();

        }

    }
}